package com.monkey.core.plugin.dto;

import com.monkey.core.MonkeyConstants;
import com.monkey.core.plugin.TypeConvertUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;

/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/9/2
 */
public class HashMonkeyDto implements MonkeyDto {

    private static Log log = LogFactory.getLog(HashMonkeyDto.class);
    private Map<String, Object> params = new HashMap<>();

    /**
     * 缺省构造函数
     */
    public HashMonkeyDto() {
    }

    /**
     * <b>创建一个带有初始化参数的Dto对象</b>
     * <p/>
     * 参数包含：1、界面提交的请求参数；
     *
     * @param request
     */
    public HashMonkeyDto(HttpServletRequest request) {
        putAll(resolveRequestParams(request.getParameterMap()));
    }

    public HashMonkeyDto(NativeWebRequest request) {
        putAll(resolveRequestParams(request.getParameterMap()));
    }


    /**
     * @param request
     * @return
     */
    @SuppressWarnings("all")
    public static Map<String, Object> resolveRequestParams(Map<String, String[]> map) {
        Map<String, Object> params = new HashMap<String, Object>();
        Iterator<String> keyIterator = (Iterator) map.keySet().iterator();
        while (keyIterator.hasNext()) {
            String key = (String) keyIterator.next();
            String value = map.get(key)[0];
            params.put(key, value);
        }
        return params;
    }


    /**
     * 以Integer类型返回属性
     *
     * @param pKey
     * @return Integer 键值
     * @throws Exception
     */
    @Override
    public Integer getInteger(String pKey) {
        Object obj = TypeConvertUtil.convert(get(pKey), "Integer", null);
        if (obj != null)
            return (Integer) obj;
        else
            return null;
    }

    /**
     * 以Long类型返回属性
     *
     * @param pKey
     * @return Long 键值
     */
    @Override
    public Long getLong(String pKey) {
        Object obj = TypeConvertUtil.convert(get(pKey), "Long", null);
        if (obj != null)
            return (Long) obj;
        else
            return null;
    }

    /**
     * 以String类型返回属性
     *
     * @param pKey
     * @return String 键值
     */
    @Override
    public String getString(String pKey) {
        Object obj = TypeConvertUtil.convert(get(pKey), "String", null);
        if (obj != null)
            return (String) obj;
        else
            return null;
    }

    /**
     * 以BigDecimal类型返回属性
     *
     * @param pKey
     * @return BigDecimal 键值
     */
    @Override
    public BigDecimal getBigDecimal(String pKey) {
        Object obj = TypeConvertUtil.convert(get(pKey), "BigDecimal", null);
        if (obj != null)
            return (BigDecimal) obj;
        else
            return null;
    }

    /**
     * 以Date类型返回属性
     *
     * @param pKey
     * @return Date 键值(yyyy-MM-dd)
     */
    @Override
    public Date getDate(String pKey) {
        Object obj = TypeConvertUtil.convert(get(pKey), "Date", "yyyy-MM-dd");
        if (obj != null)
            return (Date) obj;
        else
            return null;
    }

    /**
     * 以Timestamp类型返回属性
     *
     * @param pKey
     * @return Timestamp 键值(yyyy-MM-dd HH:mm:ss)
     */
    @Override
    public Timestamp getTimestamp(String pKey) {
        Object obj = TypeConvertUtil.convert(get(pKey), "Timestamp", "yyyy-MM-dd HH:mm:ss");
        if (obj != null)
            return (Timestamp) obj;
        else
            return null;
    }

    /**
     * 以Boolean类型返回属性
     *
     * @param pKey
     * @return Boolean 键值
     */
    @Override
    public Boolean getBoolean(String pKey) {
        Object obj = TypeConvertUtil.convert(get(pKey), "Boolean", null);
        if (obj != null)
            return (Boolean) obj;
        else
            return null;
    }

    /**
     * 以List类型返回属性
     *
     * @param pKey
     * @return List 键值
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getList(String pKey) {
        return (List<Object>) get(pKey);
    }

    /**
     * 控制台打印DTO对象,辅助调试
     */
    @Override
    public void println() {
        if (log.isDebugEnabled()) {
            System.out.println(MonkeyConstants.CONSOLE_FLAG1 + this.params);
        }
    }

    @Override
    public boolean containsValue(Object value) {
        return params.containsValue(value);
    }

    @Override
    public Object get(Object key) {
        return params.get(key);
    }

    @Override
    public Object put(String key, Object value) {
        return params.put(key, value);
    }

    @Override
    public Object remove(Object key) {
        return params.remove(key);
    }

    @Override
    public void putAll(Map<? extends String, ?> m) {
        params.putAll(m);
    }

    @Override
    public void putAll(MonkeyDto monkeyDto) {
        putAll(monkeyDto.getParams());
    }

    @Override
    public void clear() {
        params.clear();
    }

    @Override
    public Set<String> keySet() {
        return params.keySet();
    }

    @Override
    public Collection<Object> values() {
        return params.values();
    }

    @Override
    public Map<String, Object> getParams() {
        return this.params;
    }
}
